Some programming languages you might have heard of:
Creating variables
baby <- 'Meredith' sculpture <- 10
baby
## [1] "Meredith"
sculpture
## [1] 10
1, 2, 31.25, 3.783 + 2i"Research", "Platforms"TRUE and FALSE
vector1 <- 1:10
vector2 <- c("learning", "to", "code", "is", "fun")
vector1
## [1] 1 2 3 4 5 6 7 8 9 10
vector2
## [1] "learning" "to" "code" "is" "fun"
PlatoonLeads <- matrix(
c("Data Wranglers",
"Data Miners",
"Cadventurers",
"Kerry Halupka",
"Kim Doyle",
"Louise van der Werff"),
ncol=2,
dimnames=list(NULL,c("platoon","name")))
PlatoonLeads
## platoon name ## [1,] "Data Wranglers" "Kerry Halupka" ## [2,] "Data Miners" "Kim Doyle" ## [3,] "Cadventurers" "Louise van der Werff"
shoppingList <- list(
lunch = c("bread", "cheese", "tomato"),
dinner = c("frozen pizza", "chocolate mousse"),
moneyInWallet = 40.50
)
shoppingList
## $lunch ## [1] "bread" "cheese" "tomato" ## ## $dinner ## [1] "frozen pizza" "chocolate mousse" ## ## $moneyInWallet ## [1] 40.5
IMDBRatings = {"Game of Thrones": 9.4,
"Sherlock": 9.2,
"Firefly": 9.1,
"Friends": 8.9}
for movie, rating in IMDBRatings.items():
print movie + ': ' + str(rating)
## Firefly: 9.1 ## Friends: 8.9 ## Sherlock: 9.2 ## Game of Thrones: 9.4
If statements
If statements
hungry <- TRUE
if (hungry){
print("GIVE ME A TIM TAM!!!")
} else {
print("No tim tam for me, thanks!")
}
## [1] "GIVE ME A TIM TAM!!!"
If statements
hungry <- FALSE
if (hungry){
print("GIVE ME A TIM TAM!!!")
} else {
print("No tim tam for me, thanks!")
}
## [1] "No tim tam for me, thanks!"
For loops
For loops
for (beats in 1:4){
print("bring left foot to right foot")
print("step left foot back to original position")
print("bring right foot to left foot")
print("step left foot back to original position")
}
## [1] "bring left foot to right foot" ## [1] "step left foot back to original position" ## [1] "bring right foot to left foot" ## [1] "step left foot back to original position" ## [1] "bring left foot to right foot" ## [1] "step left foot back to original position" ## [1] "bring right foot to left foot" ## [1] "step left foot back to original position" ## [1] "bring left foot to right foot" ## [1] "step left foot back to original position" ## [1] "bring right foot to left foot" ## [1] "step left foot back to original position" ## [1] "bring left foot to right foot" ## [1] "step left foot back to original position" ## [1] "bring right foot to left foot" ## [1] "step left foot back to original position"
Functions
Functions
moodCalendar <- function(day){
if (day %in% c("Friday", "Saturday", "Sunday")){
print(paste(day, "is a happy day"))
} else if (day %in% c("Monday", "Tuesday")){
print(paste(day, "is a grumpy day"))
} else {
print(paste(day, "is a neutral day"))
}}
moodCalendar("Monday")
## [1] "Monday is a grumpy day"
Functions
moodCalendar <- function(day){
if (day %in% c("Friday", "Saturday", "Sunday")){
print(paste(day, "is a happy day"))
} else if (day %in% c("Monday", "Tuesday")){
print(paste(day, "is a grumpy day"))
} else {
print(paste(day, "is a neutral day"))
}}
moodCalendar("Friday")
## [1] "Friday is a happy day"